package com.tuanzi.loan.web.bean.galaxycard;

import com.google.common.collect.Lists;
import com.tuanzi.loan.business.entity.galaxycard.GalaxyCardPrivilege;
import com.tuanzi.loan.business.repository.galaxycard.impl.GalaxyCardPrivilegeRepositoryImpl;
import com.tuanzi.loan.business.service.galaxycard.GalaxyCardPrivilegeService;
import com.tuanzi.loan.core.aspect.Debug;
import com.tuanzi.loan.web.configuration.JSFListingBean;

import com.tuanzi.loan.vo.request.galaxycard.GalaxyCardPrivilegeSearchRequest;
import com.tuanzi.loan.vo.response.galaxycard.GalaxyCardPrivilegeSearchResponse;
import com.tuanzi.loan.web.lazy.galaxycard.GalaxyCardPrivilegeDataModel;

import lombok.Data;
import lombok.EqualsAndHashCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import javax.faces.bean.ViewScoped;

import static com.tuanzi.loan.business.constants.DictionaryCode.GALAXY_CARD_PRIVILEGE_STATUS;

import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 权益管理
 * @author JinRuiyang 2018/2/8 16:08:42
 */
@Debug
@Data
@Component
@ViewScoped
@EqualsAndHashCode(callSuper = true)
public class GalaxyCardPrivilegeBean extends JSFListingBean<GalaxyCardPrivilegeSearchRequest, GalaxyCardPrivilegeSearchResponse> {

	private static final long serialVersionUID = 2055892440844653275L;

	@Autowired
    private GalaxyCardPrivilegeService service;

    private Map<String, Integer> privilegeTypeList;
    private Map<String, String>  privilegeCodeList;
    private Map<String, Long>    cardTypeList;

    private GalaxyCardPrivilege selectedPrivilege;
    private Long selectedCardType;

    private GalaxyCardPrivilege addingPrivilege = new GalaxyCardPrivilege();
    private String status1Show;
    private String status1Target;

    @PostConstruct
    private void init() {
        initLazyDataModel();
        privilegeTypeList = GalaxyCardPrivilegeRepositoryImpl.PrivilegeType.toMap();
        privilegeCodeList = GalaxyCardPrivilegeRepositoryImpl.PrivilegeCode.toMap();
        cardTypeList = service.getCardType();
    }

    public void onSelect(GalaxyCardPrivilegeSearchResponse selected){
        this.selectedPrivilege = service.findById(selected.getPk());
    }

    public void onDownPrivilege() {
        if (Objects.isNull(selectedCardType))
            addErrorMessage("请先选择要操作的卡类型");
        else{
            try {
                service.unrefPrivilegeToCardType(selectedPrivilege.getId(), selectedCardType);
                addSuccessfullyMessage();
            } catch (Exception e){
                addInfoMessage(e.getMessage());
            }
        }
    }

    public void onUpPrivilege() {
        if (Objects.isNull(selectedCardType))
            addErrorMessage("请先选择要操作的卡类型");
        else{
            try {
                service.refPrivilegeToCardType(selectedPrivilege.getId(), selectedCardType);
                addSuccessfullyMessage();
            } catch (Exception e){
                addInfoMessage(e.getMessage());
            }
        }
    }

    public void onReset() {
        initLazyDataModel();
    }

    public void onEditPrivilege(){
        try {
            service.save(selectedPrivilege);
            addSuccessfullyMessage();
        }
        catch (Exception e){
            addErrorMessage("编辑失败" + e.getMessage());
        }
    }

    public void onAddPrivilege(){
        try {
            if (Objects.isNull(addingPrivilege))
                throw new NullPointerException("系统错误请刷新重试");
            if (Objects.isNull(addingPrivilege.getName()))
                throw new NullPointerException("请填写权益名称");
            if (Objects.isNull(addingPrivilege.getCode()))
                throw new NullPointerException("请选择权益类型");
            if (Objects.isNull(addingPrivilege.getPrivilegeType()))
                throw new NullPointerException("请选择权益分类");
            if (Objects.isNull(addingPrivilege.getIconUrl()))
                throw new NullPointerException("请填写权益图标链接");
            if (Objects.isNull(addingPrivilege.getProjectName()))
                addingPrivilege.setProjectName(addingPrivilege.getName());
            if (isDefaultBusiness()){
                if (Objects.isNull(status1Show))
                    throw new NullPointerException("请填写权益显示状态");
                if (Objects.isNull(status1Target))
                    throw new NullPointerException("请填写权益点击跳转链接");
                service.addPrivilege(addingPrivilege, status1Show, status1Target);
                status1Show = null;
                status1Target = null;
            }
            else
                service.addPrivilege(addingPrivilege);
            addSuccessfullyMessage();
            this.addingPrivilege = new GalaxyCardPrivilege();
        }
        catch (Exception e){
            addErrorMessage("添加权益失败：" + e.getMessage());
        }
    }

    public void onRefresh(){
        lazyDataModel = new GalaxyCardPrivilegeDataModel($);
        addSuccessfullyMessage();
    }

    private void initLazyDataModel() {
        $ = new GalaxyCardPrivilegeSearchRequest();
        lazyDataModel = new GalaxyCardPrivilegeDataModel($);
    }

    public Boolean isDefaultBusiness(){
        return GalaxyCardPrivilegeRepositoryImpl.PrivilegeCode.DEFAULT_BUSINESS.getCode()
                .equals(addingPrivilege.getCode());
    }

    public String onShowStatus(Long id) {
        return redirect("/views/galaxycard/galaxy-card-privilege-status-list.xhtml?privilege_id=" + id);
    }
    
    @Override
	protected List<String> getDictionaryCodes() {
		return Lists.newArrayList(GALAXY_CARD_PRIVILEGE_STATUS);
	}
}